const app = getApp()
var util = require('../../../../utils/util.js');
var goBack = '', _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brandAll: app.globalData.xieYouUserInfoRoot.brandAll,
    ModelByBrand: null,
    brandChoose: null,
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    if (options.goBack) {
      goBack = options.goBack
    }
    if (options.master){
      _this.setData({
        master: options.master,
        masterIndex: options.index
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showLoading({
      title: '加载中',
    })
    // 获取品牌
    _this.getAllBrand(function(e){
      _this.getModelByBrand(e);
    });
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  //获取所有品牌
  getAllBrand(callback) {
    if (!app.globalData.xieYouUserInfoRoot.brandAll) {
      app.getAllBrand(function (res) {
        if (res) {
          var brandAll = res;
          app.globalData.xieYouUserInfoRoot.brandAll = brandAll['挖掘机'];
          _this.setData({
            brandAll: brandAll['挖掘机'],
            brandImg: brandAll['挖掘机'][0].brandLogo
          })
          callback(brandAll['挖掘机'][0].id);
        }
      });
    }else{
      var brandAll = app.globalData.xieYouUserInfoRoot.brandAll
      _this.setData({
        brandAll: brandAll,
        brandImg: brandAll[0].brandLogo
      })
      callback(brandAll[0].id);
    }
  },
  // 获取品牌对应的型号
  getModelByBrand(e){
    var userCode = app.globalData.xieYouUserInfo.code;
    var BrandId = '', brandImg='';
    if (e.target){
      BrandId = e.target.dataset.brandId ? e.target.dataset.brandId : e.currentTarget.dataset.brandId;
      brandImg = e.target.dataset.brandImg ? e.target.dataset.brandImg : e.currentTarget.dataset.brandImg;
    }else{
      BrandId = e;
      brandImg = _this.data.brandImg;
    }
    let index = e.currentTarget ? e.currentTarget.dataset.index : 0;
    _this.setData({
      brandChoose: BrandId,
      brandImg: brandImg,
      csBrand: _this.data.brandAll[index]
    })
    // 获取机型编码
    app.getModelByBrand(BrandId ,function (res) {
      if (res) {
        _this.setData({
          ModelByBrand: res
        })
      }
      // 取消加载中
      wx.hideLoading();
    });
  },
  bindTapFun:function(e){
    let master = _this.data.master;
    // 是否是从设备主过来的
    if (master) {
      var index = e.currentTarget.dataset.index;
      let pages = getCurrentPages();//当前页面
      let prevPage = pages[pages.length - 2];//上一页面
      let newList = prevPage.data.newList;
      let masterIndex = _this.data.masterIndex;
      newList[masterIndex].deviceBrandModel = _this.data.ModelByBrand[index];
      newList[masterIndex].deviceBrand = _this.data.csBrand;
      prevPage.setData({//直接给上移页面赋值
        newList: newList,
      });
      if (!_this.data.buttonClicked) {
        util.buttonClicked(_this);
        wx.navigateBack({
          delta: 1
        })
      }
    } else {
      _this.addBrandModel(e);
    }
  },
  // 添加机型
  addBrandModel(e){
    var userCode = app.globalData.xieYouUserInfo.code;
    var brandModelId = e.currentTarget.dataset.brandModelId;
    var brandModelName = e.currentTarget.dataset.brandName;
    // 2018-5-22 添加填写工时页面
    // 进入填写工时页面
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        url: 'brand-word-hour/brand-word-hour?brandImg=' + _this.data.brandImg + '&brandModelId=' + brandModelId + '&userCode=' + userCode + '&brandModelName=' + brandModelName + '&goBack=' + goBack,
      })
    }
    // app.addBrandModel(userCode, brandModelId, function(res){
    //   if (res.message == 'repeat'){
    //     util.alertMsg('警告', '该机型已经添加过', function () {
    //       console.log('用户点击确定')
    //     }, function () {
    //       console.log('用户点击取消')
    //     });
    //     return;
    //   }
    //   if (res.status == 'success') {
        
    //     //_this.initUserBrandModelFun(userCode);
    //   }
    // })
  },
  // 修改绑定机型数据
  initUserBrandModelFun(code) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    if (typeof code == 'string') {
      var userCode = code
    } else {
      var userCode = code.code
    }
    app.globalData.xieYouUserInfoRoot.userBrandsModel = [];
    app.initUserBrandModel(userCode, function (data) {
      console.log('app初始化绑定机型');
      for (var i = 0; i < data.length; i++) {
        //将用户机型数据存入到全局数组
        app.globalData.xieYouUserInfoRoot.userBrandsModel.push(data[i]);
        // 将选中的机型显示到选择框中
        if (data[i].selected == 1) {
          console.log(data[i]);
          app.globalData.xieYouUserInfoRoot.userBrandModel = data[i]
        }
      }
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];   //当前页面
      var prevPage = pages[pages.length - 2];  //上一个页面
      var prevPageTow = pages[pages.length - 3];
      prevPage.setData({
        userBrandsModel: app.globalData.xieYouUserInfoRoot.userBrandsModel
      })
      if (goBack == 'index') {
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPageTow.setData({
          userBrandModel: app.globalData.xieYouUserInfoRoot.userBrandModel,
          brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
          brandModelName: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.name
        })
      } else if (goBack == 'recommend') {
        
        prevPageTow.setData({
          brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
          brandModelName: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.name
        })
        var prevPageThree = pages[pages.length - 4];
        prevPageThree.setData({
          userBrandModel: app.globalData.xieYouUserInfoRoot.userBrandModel,
          brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
          brandModelName: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.name
        })
      }
      if (!_this.data.buttonClicked) {
        util.buttonClicked(_this);
        wx.navigateBack({
          delta: 1
        })
      }
      // wx.redirectTo({ 
      //   url: '../user-model?time=2'
      // })

    });
  }
})

